package com.document.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.document.common.annotation.SysLog;
import com.document.common.exception.SoException;
import com.document.common.utils.DataResult;
import com.document.common.utils.DateUtils;
import com.document.common.utils.ExportExcelUtils;
import com.document.entity.po.FileManageEntity;
import com.document.entity.request.FileManageRequest;
import com.document.service.iservice.FileManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@RestController
@RequestMapping("/fileManage")
@Api(tags = "文件管理接口")
public class FileManageController {

    @Autowired
    private FileManageService fileManageService;



    /**
     * 所有文件列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "所有文件列表", httpMethod = "POST")
    public DataResult list(@RequestBody FileManageRequest request) {
        IPage<FileManageEntity> list = fileManageService.queryAll(request);
        return DataResult.success(list);
    }

    /**
     * 删除文件
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @SysLog(value = "删除文件")
    @ApiOperation(value = "删除文件", httpMethod = "POST")
    public DataResult delete(@RequestBody String[] ids) {
        try {
            fileManageService.deleteById(ids);
            return DataResult.success();
        }
        catch (SoException exception) {
            return DataResult.error(exception.getMsg());
        }
    }

    /**
     * 上传文件
     */
    @PostMapping("/save")
    @SysLog(value = "上传文件")
    @ApiOperation(value = "上传文件", httpMethod = "POST")
    public DataResult save(@RequestBody FileManageRequest request) {
        try {
            int count = fileManageService.save(request);
            if (count > 0) {
                return DataResult.success();
            }
            else {
                return DataResult.error("保存公告信息失败！");
            }
        }
        catch (SoException exception) {
            return DataResult.error(exception.getMsg());
        }
    }


    /**
     * 修改文件
     */
    @PostMapping("/update")
    @SysLog(value = "修改文件")
    @ApiOperation(value = "修改文件", httpMethod = "POST")
    public DataResult update(@RequestBody FileManageRequest request) {
        try {
            int count = fileManageService.update(request);
            if (count > 0) {
                return DataResult.success();
            }
            else {
                return DataResult.error("修改公告信息失败！");
            }
        }
        catch (SoException exception) {
            return DataResult.error(exception.getMsg());
        }
    }

    /**
     * 根据Id查询文件列表
     */
    @PostMapping("/selectById")
    @ApiOperation(value = "根据Id查询文件列表", httpMethod = "POST")
    public DataResult selectOneById(@RequestBody FileManageRequest request) {
        FileManageEntity info = fileManageService.selectOneById(request);
        return DataResult.success(info);
    }

    /**
     * 浏览文件
     */
    @PostMapping("/review")
    @ApiOperation(value = "浏览文件", httpMethod = "POST")
    public DataResult review(@RequestBody FileManageRequest request) {
        FileManageEntity info = fileManageService.review(request);
        return DataResult.success(info);
    }

    /**
     * 导出文件
     */
    @PostMapping("/export")
    @ApiOperation(value = "导出文件", httpMethod = "POST",produces="application/octet-stream")
    public void export(@RequestBody FileManageRequest request, HttpServletResponse response) throws Exception {
        IPage<FileManageEntity> page = fileManageService.queryAll(request);
        //url编码防止前端出现中文乱码情况
        String fileName= "文档数据"+ DateUtils.format(new Date(),"yyyyMMddHHmmss")+".xls";
        ExportExcelUtils.exportExcel(FileManageEntity.class,page.getRecords(),fileName,response);
    }


}
